package top.kaoshanji.p4ch28c;

import java.util.HashMap;

/**
 * 代码清单-p4ch28c-364页a：享元工厂
 * @author kaoshanji
 * ^_^
 * create time 2025/3/28 11:30
 */
public class FlyweightFactory {

    // 定义一个池容器
    private static HashMap<String, Flyweight> pool = new HashMap<>();

    // 享元工厂
    public static Flyweight getFlyweight(String extrinsic) {

        // 需要返回的对象
        Flyweight flyweight = null;
        // 在池中有该对象
        if (pool.containsKey(extrinsic)) {
            flyweight = pool.get(extrinsic);
        } else {
            // 根据外部状态创建享元对象
            flyweight = new ConcreteFlyweight1(extrinsic);
            // 放置在池中
            pool.put(extrinsic, flyweight);
        }

        return flyweight;
    }

}
